package com.jiepu.daily.corejava.advanceed.part7io;


import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @Description:
 * @author: xinjie.txt
 * @date: 2020/8/4  15:43
 */
public class FileDemo3Test {
	public static void main(String[] args) {
	}

	/**
	 * 创建 文件
	 */
	@Test
	public void test1() {
		File file = new File("src/com/jiepu/daily/advanceed/part7_io/io_test/xinjie.txt");
		System.out.println(file.exists());
		if (!file.exists()) {
			System.out.println("File no_exist...");
			try {
				boolean result = file.createNewFile();
				System.out.println(result);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} // fi
		else {
			System.out.println("File existed");
		}

	}

	/**
	 * 创建 文件夹
	 */
	@Test
	public void test2() {
		File file = new File("src/com/jiepu/daily/advanceed/part7_io/io_test");
		boolean result = file.mkdir();
		if (result) {
			System.out.println(file + " OK !");
		}
	}

	/**
	 * 重命名文件
	 */
	@Test
	public void test3() {
		File file = new File("src/com/jiepu/daily/advanceed/part7_io/io_test/xinjie.txt.txt");
		File renamefile = new File("src/com/jiepu/daily/advanceed/part7_io/io_test/xin.txt");
		file.renameTo(renamefile);
	}

	/**
	 *
	 */
	@Test
	public void test4() {
		File file = new File("src/com/jiepu/daily/advanceed/part7_io/io_test/xinjie.txt");
		System.out.println("Input ：  " + file + "");
		File parentFile = file.getParentFile().getParentFile();
		File absoluteFile = file.getAbsoluteFile();
		System.out.println("绝对路径：  " + absoluteFile);
		System.out.println("相对路径 ：  " + parentFile);

	}

	@Test
	public void test5() {
		/**
		 * 输出 当前目录 所有文件
		 */
		File file = new File("src/com/jiepu/daily/advanceed/part7_io");
		File[] files = file.listFiles();
		for (File f : files
		) {
			System.out.println(f);

			// 嵌套 打印 下一级

		}

		/**
		 * 输出 盘符
		 */
		File[] roots = File.listRoots();
		for (File root : roots
		) {
			System.out.println(root);
		}

	}

	@Test
	public void test6() {
		System.out.println(add(100));
		System.out.println("count: " + count);
	}

	int count = 0;

	private int add(int i) {

		count++;
		if (i <= 1) {
			return 1;
		}

		return add(i - 1) + 2;
	}


	@Test
	public void test7() {
		File dir = new File("src/com/jiepu/daily/advanceed/part7_io");
		// 获取dir下的所有 直接子文件&&子目录
		File[] files = dir.listFiles();
		for (File f : files
		) {
			// getName
			System.out.println("FileName:" + f.getName());

		}

	}

	@Test
	public void test8() {
		print(1);
	}

	private void print(int i) {
		System.out.println(i);
		if (i == 10) {
			return;
		}
		print(i + 1);
	}

	/**
	 * 100++1
	 */
	@Test
	public void test9() {
		System.out.println(add9(100));

	}

	private int add9(int i) {

		if (i == 1) {
			return 1;
		}
		System.out.println(i);
		return add9(i - 1) + i;
	}

	@Test
	public void test10() {
		System.out.println(add10(1));
	}

	private int add10(int i) {
		return i == 100 ? 100 : add10(i + 1) + i;
	}




}
